﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.ComponentModel;

namespace DAO
{
    public class AccountDAO
    {
        public AccountDTO CreateAccountDTOFromEntity(NguoiDung nd)
        {
            try
            {
                AccountDTO Acc = new AccountDTO
                {
                    EmployeeID = nd.NhanVienID,
                    Password = nd.MatKhau ?? string.Empty,
                    UserName = nd.TaiKhoan ?? string.Empty
                };

                if (nd.NhanVienID > 0)
                {
                    EmployeeDAO empDAO = new EmployeeDAO();
                    EmployeeDTO empDTO = empDAO.GetEmployeeByID(nd.NhanVienID);

                    Acc.EmployeeName = empDTO.EmployeeName;
                    Acc.EmPloyeeCard = empDTO.EmployeeCMND;
                }

                return Acc;
            }
            catch
            {
                return null;
            }
        }

        public int InsertAccount(AccountDTO Acc)
        {
            try
            {
                using( BookStoreEntities ent = new BookStoreEntities())
                {
                    NguoiDung nd = new NguoiDung
                    {
                        NhanVienID = Acc.EmployeeID,
                        TaiKhoan = Acc.UserName,
                        MatKhau =Acc.Password
                    };

                    if (ent.NguoiDungs.Where(n => n.TaiKhoan == nd.TaiKhoan).Count() > 0)
                    {
                        return -1;
                    }
                    else if (ent.NguoiDungs.Where(n => n.NhanVienID == nd.NhanVienID).Count() > 0)
                    {
                        return -1;
                    }

                    ent.AddToNguoiDungs(nd);
                    ent.SaveChanges();
                    return Acc.EmployeeID;
                }
            }
            catch
            {
                return 0;
            }
        }

        public bool UpdateAccount(AccountDTO Acc)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    NguoiDung nd = ent.NguoiDungs.FirstOrDefault(n => n.NhanVienID == Acc.EmployeeID);

                    nd.MatKhau = Acc.Password;

                    ent.SaveChanges();

                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool DeleteAccount(int ID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    NguoiDung nd = ent.NguoiDungs.FirstOrDefault(n => n.NhanVienID == ID);

                    ent.NguoiDungs.DeleteObject(nd);
                    ent.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public BindingList<AccountDTO> GetNewBindingList()
        {
            try
            {
                BindingList<AccountDTO> ListAcc = new BindingList<AccountDTO>();

                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    List<NguoiDung> listND = new List<NguoiDung>();
                    listND = ent.NguoiDungs.ToList();

                    foreach (NguoiDung nd in listND)
                    {
                        ListAcc.Add(CreateAccountDTOFromEntity(nd));
                    }
                }

                return ListAcc;
            }
            catch
            {
                return null;
            }
        }

        public AccountDTO GetAccountByID(int ID)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    AccountDTO Acc = new AccountDTO();

                    NguoiDung nd = ent.NguoiDungs.FirstOrDefault(n => n.NhanVienID == ID);

                    Acc = CreateAccountDTOFromEntity(nd);

                    return Acc;
                }
            }
            catch
            {
                return null;
            }
        }

        public bool Login(string userName, string password, out AccountDTO acc)
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    acc = new AccountDTO();

                    if (ent.NguoiDungs.Where(n => n.TaiKhoan == userName && n.MatKhau == password).Count() > 0)
                    {
                        NguoiDung nd = ent.NguoiDungs.FirstOrDefault(n => n.TaiKhoan == userName && n.MatKhau == password);
                        acc = CreateAccountDTOFromEntity(nd);
                        return true;
                    }
                    else
                    {
                        acc = null;
                        return false;
                    }
                }
            }
            catch
            {
                acc = null;
                return false;
            }
        }
    }
}
